package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushougeproduct.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.param.SpuAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.SpuUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.SpuFullInfoVO;
import cn.tedu.yushougeproduct.pojo.vo.SpuListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.SpuStandardVO;
import cn.tedu.yushougeproduct.service.ISpuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/spu")
@Api(tags = "12. SPU模块")
@Validated
@Slf4j
public class SpuController {

    @Autowired
    ISpuService spuService;

    @PostMapping("/add-new")
    @ApiOperation("添加SPU")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid SpuAddNewParam spuAddNewParam) {
        log.debug("开始处理【添加SPU】的请求，参数：{}", spuAddNewParam);
        spuService.addNew(spuAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("Published")
    @ApiOperation("上架商品")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "spuID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisplay(@Range(min = 1, message = "请提交有效的spuID值！") @RequestParam Long id) {
        log.debug("开始处理【上架商品】的请求，参数：{}", id);
        spuService.setPublished(id);
        return JsonResult.ok();
    }

    @PostMapping("notPublished")
    @ApiOperation("下架商品")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "spuID", required = true, dataType = "long")
    })
    public JsonResult<Void> setHide(@Range(min = 1, message = "请提交有效的spuID值！") @RequestParam Long id) {
        log.debug("开始处理【下架商品】的请求，参数：{}", id);
        spuService.setNotPublished(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改spu")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "spuID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的spuID值！") Long id,
                                           @Valid SpuUpdateInfoParam updateInfoParam) {
        log.debug("开始处理【修改spu详情】的请求，ID：{}", id);
        spuService.updateInfoById(id, updateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询SPU详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "spuID", required = true, dataType = "long")
    })
    public JsonResult<SpuStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询SPU详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询SPU详情】的请求，参数：{}", id);
        SpuStandardVO result = spuService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("")
    @ApiOperation("查询SPU")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<SpuListItemVO>> list(@Range(min = 1, message = "查询SPU失败，请提供正确的页码值！")
                                                                Integer pageNum) {
        log.debug("开始处理【查询SPU列表】的请求，页码：{}", pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<SpuListItemVO> pageData = spuService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/listByBrandId")
    @ApiOperation("根据品牌Id查询SPU")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<SpuListItemVO>> listByBrandId(@Range(min = 1, message = "查询SPU失败，请提供正确的页码值！") Integer pageNum,
                                                             @RequestParam Long brandId) {
        log.debug("开始处理【根据品牌Id查询SPU列表】的请求，页码：{},品牌Id：{}", pageNum,brandId);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<SpuListItemVO> pageData = spuService.listByBrandId(pageNum,brandId);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/listByCategoryId")
    @ApiOperation("根据类别Id查询SPU")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<SpuListItemVO>> listByCategoryId(@Range(min = 1, message = "查询SPU失败，请提供正确的页码值！") Integer pageNum,
                                                             @RequestParam Long categoryId) {
        log.debug("开始处理【根据类别Id查询SPU列表】的请求，页码：{}，类别Id：{}", pageNum,categoryId);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<SpuListItemVO> pageData = spuService.listByCategoryId(pageNum,categoryId);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/listByKeywords")
    @ApiOperation("根据关键字查询SPU")
    @ApiOperationSupport(order = 460)
    public JsonResult<List<SpuListItemVO>> listByKeywords(String keyword) {
        log.debug("开始处理【根据关键字查询SPU列表】的请求，关键字：{}", keyword);
        List<SpuListItemVO> list = spuService.listByKeywords(keyword);
        return JsonResult.ok(list);
    }

}
